package springboot.client;


import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.springframework.stereotype.Service;
import springboot.kernel.KernelTools;
import springboot.wstool.DateConvert;
import springboot.wstool.WSTools;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


@Service
@Slf4j
public class TaskStatusAndAgvStatus {

    public static final String V1="Vehicle-0001";
    private boolean bindOrderNameWithAgv=false;
    private String orderName=null;
    private boolean readyToLoad=false;
    private boolean readyToUnload=false;
    private boolean finished=false;
    private boolean failed=false;
    private boolean sendWith80 = false;
    private boolean sendWith81 = false;
    private boolean sendWith82 = false;
    private Timer dogCheckTimer = null;

    @Resource
    private DateConvert dateConvert;
    @Resource
    private WSTools wsTools;
    @Resource
    private KernelTools opentcsClient;

    @PostConstruct
    private void firstStart(){
        opentcsClient.getConnect();//先连接
        timerChangeTaskStatus();//后判断
    }

    private void timerChangeTaskStatus() {
        //check task state period 2s
        if(dogCheckTimer == null) {
            dogCheckTimer = new Timer();
        }
        dogCheckTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (opentcsClient.getAgvHasTask(V1)){
                    if (!bindOrderNameWithAgv) {
                        //注意绑定只能绑定一次，不能一直绑定，因为任务结束之后就无法通过agv获取任务名
                        orderName = opentcsClient.getOrderName(V1);//任务名称
                        bindOrderNameWithAgv = true;//每次绑定之后，都清理上一次占位
                        readyToLoad = false;
                        readyToUnload  = false;
                        finished = false;
                        failed = false;
                        sendWith80 = false;
                        sendWith81 = false;
                        sendWith82 = false;
                    }
                    if (bindOrderNameWithAgv){
                        if (!sendWith80 && opentcsClient.getError(V1,"load_failed")==1){
                            log.info("80==load not goods");

                            try {
                                Client client= wsTools.getClient();
                                client.invoke("receiveTaskState",orderName,80,"0001",dateConvert.convertToXMLGregorianCalendar(new Date()),0);
                                client.destroy();
                                sendWith80 = true;
                                log.info("80 response");
                            }catch (Exception e){
                                log.info("error is at 80 {}",e);
                                e.printStackTrace();
                            }
                        }else if (!sendWith81 && opentcsClient.getError(V1,"unload_failed")==1){
                            log.info("81==unload but has goods");

                            try {
                                Client client= wsTools.getClient();
                                client.invoke("receiveTaskState",orderName,81,"0001",dateConvert.convertToXMLGregorianCalendar(new Date()),0);
                                client.destroy();
                                sendWith81 = true;
                                log.info("81 response");
                            }catch (Exception e){
                                log.info("error is at 81 {}",e);
                                e.printStackTrace();
                            }
                        }else if (!sendWith82 && opentcsClient.getError(V1,"s300_5minutes")==1){
                            log.info("82==S300 trigger more than 5 minutes");
                            try {
                                Client client= wsTools.getClient();
                                client.invoke("receiveTaskState",orderName,82,"0001",dateConvert.convertToXMLGregorianCalendar(new Date()),0);
                                client.destroy();
                                sendWith82 = true;
                                log.info("82 response");
                            }catch (Exception e){
                                log.info("error is at 82 {}",e);
                                e.printStackTrace();
                            }
                        }
                        if (!readyToLoad){
                            log.info("20==receive task");
                            try {
                                Client client= wsTools.getClient();
                                client.invoke("receiveTaskState",orderName,20,"0001",dateConvert.convertToXMLGregorianCalendar(new Date()),0);
                                client.destroy();
                                readyToLoad=true;
                                log.info("20 response");
                            }catch (Exception e){
                                log.info("error is at 20 {}",e);
                                e.printStackTrace();
                            }
                        }else if (!readyToUnload && opentcsClient.getReadyToUnload(V1,orderName)){
                            log.info("30==load finished");
                            try {
                                Client client= wsTools.getClient();
                                client.invoke("receiveTaskState",orderName,30,"0001",dateConvert.convertToXMLGregorianCalendar(new Date()),0);
                                client.destroy();
                                readyToUnload=true;
                                log.info("30 response");
                            }catch (Exception e){
                                log.info("error is at 30 {}",e);
                                e.printStackTrace();
                            }
                        }
                    }
                }
                log.info("orderName is {}",orderName);
                if (bindOrderNameWithAgv){
                    if (!finished && opentcsClient.getFinished(orderName)){
                        log.info("40==unload finished");
                        try {
                            Client client= wsTools.getClient();
                            client.invoke("receiveTaskState",orderName,40,"0001",dateConvert.convertToXMLGregorianCalendar(new Date()),1);
                            client.destroy();
                            bindOrderNameWithAgv=false;//完成就进行解绑
                            finished=true;
                            log.info("40 response");
                        }catch (Exception e){
                            log.info("error is at 40 {}",e);
                            e.printStackTrace();
                        }
                    }else if (!failed && opentcsClient.getFailed(orderName)){
                        log.info("99==task failed");
                        try {
                            Client client= wsTools.getClient();
                            client.invoke("receiveTaskState",orderName,99,"0001",dateConvert.convertToXMLGregorianCalendar(new Date()),1);
                            client.destroy();
                            bindOrderNameWithAgv=false;//失败就进行解绑
                            failed=true;
                            log.info("99 response");
                        }catch (Exception e){
                            log.info("error is at 99 {}",e);
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, 3000, 3000);
    }
}
